﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Controls;
using LED.DataBase;
using LED.Helper;
using SXC.Control;
using System.Windows.Forms;
namespace SXC.LED.Control
{
    public class SXCLEDControl
    {
        public static MediaElement MediaPlayer = null;
        public static LEDDataSet LocalData;
        public static LEDData m_led = new LEDData();
        public static DataRow dwLED = null;
        public static DataRow dwPROGRAM = null;
        public static string rootfolder = "";
        #region=============Interface=======================
        private static Int32 StartUp()
        {
            Int32 result = 1;
            try
            {
                rootfolder = PathHelper.ApplicationPath;
               
                LocalData.Merge(m_led.LoadLocalData());
                Int32 dev = -1;
                PDeviceParam param = new PDeviceParam();
                param.devType = LEDHelper.DEVICE_TYPE_UDP;
                param.ComPort = 4;
                param.Speed = LEDHelper.SBR_57600;
                param.FlowCon = 0;
                dev = SXCLEDControl.LED_Open(ref param, 1, 1, 1);
                result= dev;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.StackTrace, ex.Message);
            }
            return result;
        }
        public static Int32 InsigmaLEDDriver_Init()
        {

            Int32 result = 1;
            LocalData = new LEDDataSet();
            result=StartUp();
            return result;
        }
        public static void InsigmaLEDDriver_Cleanup()
        {
            try
            {

            }
            catch (Exception ex)
            {
            }
        }
        public static Int32 LED_IsDeviceOK(int nLEDID)
        {
            Int32 result = 0;
            Int32 dev = 0;
            try
            {
                byte Address = 1;
                DataRow dwLed = (DataRow)LocateLED(nLEDID);
                string Host = dwLed["LMTCPIP4ADDRESS"].ToString();
                ushort port=System.Convert.ToUInt16( dwLed["LMTCPIP4PORT"].ToString());
                SXCLEDControl.LED_Query(dev, Address, Host, port);
            }
            catch (Exception ex)
            {
                
            }
            return result;
        }
        public static Int32 LED_SwitchPower(int nLEDID, int nTurnOn)
        {
            Int32 result =1;
            Int32 dev = 0; 
            try
            {
                byte Address = 1; 
                DataRow dwLed = (DataRow)LocateLED(nLEDID);
                string Host = dwLed["LMTCPIP4ADDRESS"].ToString();
                ushort port = System.Convert.ToUInt16(dwLed["LMTCPIP4PORT"].ToString());
                LED_SetPower(dev, Address, Host, port, nTurnOn);
            }
            catch (Exception ex)
            {
                
            }
            return result;
        }
        public static Int32 LED_IsDeviceTurnOn(int nLEDID)
        { 
            Int32 result =1;
            Int32 dev = 0; 
            try
            {
                byte Address = 1; 
                DataRow dwLed = (DataRow)LocateLED(nLEDID);
                string Host = dwLed["LMTCPIP4ADDRESS"].ToString();
                ushort port = System.Convert.ToUInt16(dwLed["LMTCPIP4PORT"].ToString());
                LED_GetPower(dev, Address, Host, port);
            }
            catch (Exception ex)
            {
                
            }
            return result;
        }
        public static Int32 LED_SetBright(int nLEDID, Int32 lValue)
        {
            Int32 result = 0;
            Int32 dev = 0; 
            Byte Brightness = System.Convert.ToByte(lValue);
            try
            {

                byte Address = 1;
                DataRow dwLed = (DataRow)LocateLED(nLEDID);
                string Host = dwLed["LMTCPIP4ADDRESS"].ToString();
                ushort port = System.Convert.ToUInt16(dwLed["LMTCPIP4PORT"].ToString());
                LED_SetBrightness(dev, Address, Host, port, Brightness);
            }
            catch (Exception ex)
            {
                
            }
            return result;
        }
        public static Int32 LED_GetBright(int nLEDID)
        {
            Int32 result = 0;
            Int32 dev = 0; 
            try
            {

                byte Address = 1;
                DataRow dwLed = (DataRow)LocateLED(nLEDID);
                string Host = dwLed["LMTCPIP4ADDRESS"].ToString();
                ushort port = System.Convert.ToUInt16(dwLed["LMTCPIP4PORT"].ToString());
                LED_GetBrightness(dev, Address, Host, port);
            }
            catch (Exception ex)
            {
                
            }
            return result;
        }
        public static Int32 LED_Restart(int nLEDID)
        {
            Int32 result = 0;
            Int32 dev = 0; 
            int Power = 0;
            try
            {
                byte Address = 1;
                DataRow dwLed = (DataRow)LocateLED(nLEDID);
                string Host = dwLed["LMTCPIP4ADDRESS"].ToString();
                ushort port = System.Convert.ToUInt16(dwLed["LMTCPIP4PORT"].ToString());

                Power = 0;
                LED_SetPower(dev, Address, Host, port, Power);
                Power = 1;
                LED_SetPower(dev, Address, Host, port, Power);
                //void LEDAPI LED_SetPower(long dev, BYTE Group, BYTE ScrNo, char *Host, WORD port, DWORD Power);
                //返回：成功返回0，失败返回-1
            }
            catch (Exception ex)
            {
            }
            return result;
        }
        public static int LED_AdjustCurrentTime(int nLEDID)
        {
            Int32 result = 0;
            Int32 dev = 0; 
            try
            {
                byte Address = 1;
                DataRow dwLed = (DataRow)LocateLED(nLEDID);
                string Host = dwLed["LMTCPIP4ADDRESS"].ToString();
                ushort port = System.Convert.ToUInt16(dwLed["LMTCPIP4PORT"].ToString());
                LED_AdjustTime(dev, Address, Host, port);
            }
            catch (Exception ex)
            {
            }
            return result;
        }
        public static int LED_PreUploadBMP(int nLEDID, string szBmpFile)
        {
            #region
            /*
             long LEDAPI AddWindow(HDC dc,short width, short height, LPRECT rect, long method, long speed, long transparent);
该函数在当前显示页面上创建一个显示区域，显示内容来自于dc
参数：
	dc:  设备句柄（关于设备句柄的概念，可参见Windows API的介绍）
width:  截取的宽度
height:  截取图片的高度
rect:    显示区域(left, top, right, bottom)
method: 显示方式
   1.   立即显示
   2.   左滚显示
   3.   连续上滚
   4.   中间向上下展开
   5.   中间向两边展开
   6.   中间向四周展开
   7.   向左移入
   8.   向右移入
   9.   从左向右展开
  10.   从右向左展开
  11.   右上角移入
  12.   右下角移入
  13.   左上角移入
  14.   左下角移入
  15.   从上向下移入
  16.   从下向上移入
  17.   闪  烁
speed:   显示速度（1-8）越大越快
transparent: 是否透明   0=不透明 1=透明
返回值：
	如果创建成功，返回1，其它值为失败
	失败的原因一般为：
1.	没有初始化动态链接库（没有调用LED_Startup）
2.	没有开始形成数据（没有调用MakeRoot）
3.	不存在当前页面（没有调用AddLeaf）


             */
            #endregion
            Int32 result = 0;
            try
            {
                short width = 100;
                short height = 32;
                Int32 method = 1;
                Int32 speed = 1;
                Int32 transparent = 1;
                IntPtr platDC = GetForegroundWindow();
                IntPtr windDC = GetWindowDC(platDC);
                Rect rect = new Rect();
                GetWindowRect(windDC, ref  rect);
                Bitmap bmp = new Bitmap(rect.Right - rect.Left, rect.Bottom - rect.Top);
                Graphics g = Graphics.FromImage(bmp);
                IntPtr hdc = g.GetHdc();
                SRECT lprect = new SRECT();
                /*lprect.left = rect.Left;
                lprect.right = rect.Right;
                lprect.bottom = rect.Bottom;
                lprect.top = rect.Top;*/
                AddWindow(hdc, (Int16)width, (Int16)height, ref lprect, method, speed, transparent);
            }
            catch (Exception ex)
            {
            }
            return result;
        }

        public static int LED_RemoveUploadedBmp(int nLEDID, string szBmpFile)
        {
            int result = 0;
            Int32 dev = 0;
            byte Address = 1;
            string Host = "192.168.0.101";
            UInt16 port = 6666;
            short width = 100;
            short height = 32;
            LPRECT rect = new LPRECT();
            Int32 method = 1;
            Int32 speed = 1;
            Int32 transparent = 1;
            try
            {
                //AddWindow(dev, width, height, rect, method, speed, transparent);
            }
            catch (Exception ex)
            {
            }
            return result;
        }
        public static Int32 LED_GetFreeUploadBmpSpace(int nLEDID)
        {
            Int32 result = 0;

            try
            {

            }
            catch (Exception ex)
            {
            }
            return result;
        }
        public static Int32 LED_LEDScreenInit(int nLEDID, int nScreenType, int nWidth, int nHeight)
        {
            Int32 result = 0;

            try
            {
                //创建一个发送序列,以前的将被清除
                //ROOT_DOWNLOAD
                //ROOT_PLAY: 表示显示内容仅播放，无需保存，控制卡掉电后不再需要显示
                //ROOT_DOWNLOAD: 表示显示内容需要保存在主板中，当控制卡上电后自动播放
                //           (注意：控制卡只能保存1M的数据)
                MakeRoot(LEDHelper.ROOT_PLAY, LEDHelper.SCREEN_COLOR);
            }
            catch (Exception ex)
            {
            }
            return result;
        }
        public static Int32 LED_LEDClear(int nLEDID)
        {
            Int32 result = 0;

            try
            {

            }
            catch (Exception ex)
            {
            }
            return result;
        }
        public static object LocateLED(int nLEDID)
        {
            object result = null;
            try
            {
                string expression = "LMLEDID='" + nLEDID + "'";
                DataRow[] rows = LocalData.MASTER.Select(expression);
                foreach (DataRow dwTemp in rows)
                {
                    dwLED = dwTemp;
                }
                result = dwLED;
            }
            catch (Exception ex)
            {
                
            }
            return result;
        }
        public static Int32 LED_BeginProgram(int nLEDID, Int32 lDisplayTime, string szBackBMP, int nProgramID = 0)
        {
            Int32 result = 0;
            try
            {
                LocateLED(nLEDID);
                //增加一个帧
                //PRSYSTEM	 PRLEDID	PRPROGRAMID
                //PRSCREENTYPE	 PRDISPLAYTIME
                string expression = "PRSYSTEM='000' and PRLEDID='" + nLEDID + "' and PRPROGRAMID='" + nProgramID + "'";
                DataRow[] rows = LocalData.PROGRAM.Select(expression);
                foreach (DataRow dwTemp in rows)
                {
                    dwPROGRAM = dwTemp;
                }
                Int32 RootType = LEDHelper.ROOT_PLAY;
                Int32 ScreenType = LEDHelper.SCREEN_COLOR;

                if (dwPROGRAM != null)
                {
                    switch (dwPROGRAM["PRPLAYTYPE"].ToString())
                    {
                        case "ROOT_DOWNLOAD":
                            RootType = LEDHelper.ROOT_DOWNLOAD;
                            break;
                        case "ROOT_PLAY":
                        default:
                            RootType = LEDHelper.ROOT_PLAY;
                            break;
                    }
                    switch (dwPROGRAM["PRSCREENTYPE"].ToString())
                    {
                        case "SCREEN_UNICOLOR":
                            ScreenType = LEDHelper.SCREEN_UNICOLOR;
                            break;
                        case "SCREEN_FULLCOLOR":
                            ScreenType = LEDHelper.SCREEN_FULLCOLOR;
                            break;
                        case "SCREEN_GRAY":
                            ScreenType = LEDHelper.SCREEN_GRAY;
                            break;
                        case "SCREEN_COLOR":
                        default:
                            ScreenType = LEDHelper.SCREEN_COLOR;
                            break;
                    }
                    MakeRoot(RootType, ScreenType);
                }                
                if (nProgramID == 0)
                {
                    AddLeaf(lDisplayTime);
                }
            }
            catch (Exception ex)
            {
            }
            return result;
        }
        public static Int32 LED_ComposeProgram(int nLEDID, int nProgramID)
        {
            Int32 result = 0;
            try
            {
                /*
                 LFSYSTEM	LFLEDID	LFPROGRAMID	 LFLEAFID	 LFLEAFNAME LFDURATION
                 */
                string expression = "LFSYSTEM='000' and LFLEDID='" + nLEDID + "' and LFPROGRAMID='" + nProgramID + "'";
                DataRow[] rows = LocalData.LEAF.Select(expression);
                int Displaytime = 1000;
                foreach (DataRow dwLEAF in rows)
                {
                    Displaytime = System.Convert.ToInt32(dwLEAF["LFDURATION"].ToString());
                    AddLeaf(Displaytime);
                    string sys = dwLEAF["LFSYSTEM"].ToString();
                    string led = dwLEAF["LFLEDID"].ToString();
                    string pgid = dwLEAF["LFPROGRAMID"].ToString();
                    string lfid = dwLEAF["LFLEAFID"].ToString();
                    expression = "LDSYSTEM='" + sys + "' and LDLEDID='" + led + "' and LDPROGRAMID='" + pgid + "'";
                    DataRow[] rowd = LocalData.LEAFDETAIL.Select(expression);
                    foreach (DataRow dwDetail in rowd)
                    {
                        AddLeafDeatail(dwDetail, led, sys);
                    }
                }
            }
            catch (Exception ex)
            {
            }
            return result;
        }
        public static void AddLeafDeatail(DataRow dwLeafDetail, string led, string sys,DataRow dwCon=null)
        {
            try
            { 
                string expression = "";
                string cind = "";
                string ctype = "";
                int ltopx = 0, ltopy = 0, rtopx = 0, rtopy = 0, lbx = 0, lby = 0, rbx = 0, rby = 0;
                int nLEDID = System.Convert.ToInt32(led);
                DataRow dwContent = null;
                DataRow[] dwX = null;
                LEDBlock rect = new LEDBlock();
                int Displaytime = 1000;

                #region========Retrive=============
                cind = dwLeafDetail["LDCONTENTID"].ToString();
                ctype = dwLeafDetail["LDCONTENTTYPE"].ToString();
                ltopx = System.Convert.ToInt32(dwLeafDetail["LDLTOPX"].ToString());
                ltopy = System.Convert.ToInt32(dwLeafDetail["LDLTOPY"].ToString());
                rtopx = System.Convert.ToInt32(dwLeafDetail["LDRTOPX"].ToString());
                rtopy = System.Convert.ToInt32(dwLeafDetail["LDRTOPY"].ToString());
                lbx = System.Convert.ToInt32(dwLeafDetail["LDLBOTTOMX"].ToString());
                lby = System.Convert.ToInt32(dwLeafDetail["LDLBOTTOMY"].ToString());
                rbx = System.Convert.ToInt32(dwLeafDetail["LDRBOTTOMX"].ToString());
                rby = System.Convert.ToInt32(dwLeafDetail["LDRBOTTOMY"].ToString());
                rect.nStartX = ltopx;
                rect.nStartY = ltopy;
                rect.nWidth = rtopx - ltopx;
                rect.nHeight = rby - rtopy;
                #endregion
                dwContent = null;
                int nEffectType, nSpeed;
                Int32 lTrasnparent;
                string szBmpFile = "";
                LEDBlock destRect, sourceRect;
                string startFolder = "";
                IEnumerable<System.IO.FileInfo> fileList = null;
                switch (ctype)
                {
                    case "CTEXT":
                        expression = "CTSYSTEM='" + sys + "' and CTEXTID='" + cind + "'";
                        dwX = LocalData.CTEXT.Select(expression);
                        foreach (DataRow dwTemp in dwX)
                        {
                            dwContent = dwTemp;
                        }
                        string szText = dwContent["CTCONTENT"].ToString().Trim();
                        string cfontid = dwContent["CTFONTID"].ToString().Trim();
                        LEDFontDescriptor lpFont = GetFontDescription(cfontid);
                        nEffectType = System.Convert.ToInt32(dwContent["CTEFFECT"].ToString());
                        nSpeed = System.Convert.ToInt32(dwContent["CTSPEED"].ToString());
                        lTrasnparent = System.Convert.ToInt32(dwContent["CTTRANSPARENT"].ToString());
                        int nHAlign = 1;
                        int nVAlign = 1;
                        LED_AddTextBlock(nLEDID, szText, rect, lpFont, nHAlign, nVAlign, nEffectType, nSpeed, lTrasnparent);
                        break;
                    case "CPICTURE":
                        expression = "CPSYSTEM='" + sys + "' and CPPICTUREID='" + cind + "'";
                        MessageBox.Show(expression);
                        dwX = LocalData.CPICTURE.Select(expression);
                        foreach (DataRow dwTemp in dwX)
                        {
                            dwContent = dwTemp;
                        }
                        if (dwContent == null)
                        {
                            if (dwCon != null)
                                dwContent = dwCon;
                        }
                        if (dwContent == null)
                            return;
                        szBmpFile = dwContent["CPPICTUREFILE"].ToString().Trim();
                        destRect = new LEDBlock();
                        destRect.nStartX = rect.nStartX;
                        destRect.nStartY = rect.nStartY;
                        destRect.nWidth = rect.nWidth;
                        destRect.nHeight = rect.nHeight;
                        sourceRect = new LEDBlock();
                        sourceRect.nStartX = 0;
                        sourceRect.nStartY = 0;
                        sourceRect.nWidth = System.Convert.ToInt32(dwContent["CPWIDTH"].ToString());
                        sourceRect.nHeight = System.Convert.ToInt32(dwContent["CPHEIGHT"].ToString());
                        nEffectType = System.Convert.ToInt32(dwContent["CPEFFECT"].ToString());
                        nSpeed = System.Convert.ToInt32(dwContent["CPSPEED"].ToString());
                        lTrasnparent = System.Convert.ToInt32(dwContent["CPTRANSPARENT"].ToString());
                        MessageBox.Show(szBmpFile);
                        LED_AddBmpBlock(nLEDID, szBmpFile, destRect, sourceRect, nEffectType, nSpeed, lTrasnparent);
                        break;
                    case "CMOVIE":
                        expression = "CMSYSTEM='" + sys + "' and CMMOVIEID='" + cind + "'";
                        dwX = LocalData.CMOVIE.Select(expression);
                        foreach (DataRow dwTemp in dwX)
                        {
                            dwContent = dwTemp;
                        }
                        string CMMOVIETYPE = dwContent["CMMOVIETYPE"].ToString().Trim();
                        string mfile = dwContent["CMMOVIEFILE"].ToString().Trim();
                        string mfilename = dwContent["CMMOVIENAME"].ToString().Trim();
                        string mext = dwContent["CMFILEEXT"].ToString().Trim();
                        destRect = new LEDBlock();
                        destRect.nStartX = rect.nStartX;
                        destRect.nStartY = rect.nStartY;
                        destRect.nWidth = rect.nWidth;
                        destRect.nHeight = rect.nHeight;
                        #region=========连续图片================
                        sourceRect = new LEDBlock();
                        sourceRect.nStartX = 0;
                        sourceRect.nStartY = 0;
                        sourceRect.nWidth = System.Convert.ToInt32(dwContent["CMWIDTH"].ToString());
                        sourceRect.nHeight = System.Convert.ToInt32(dwContent["CMHEIGHT"].ToString());
                        nEffectType = System.Convert.ToInt32(dwContent["CMEFFECT"].ToString());
                        nSpeed = System.Convert.ToInt32(dwContent["CMSPEED"].ToString());
                        lTrasnparent = System.Convert.ToInt32(dwContent["CMTRANSPARENT"].ToString());
                        Displaytime = System.Convert.ToInt32(dwContent["CMDURATION"].ToString());
                        if (CMMOVIETYPE == "CPIC")
                        {
                            // D:\ZZMyWorks\SXC\SXCPlatform\SXCPlatform\Exe\LMovie\M00001
                            startFolder = rootfolder + @"\" + mfile + @"\" + mfilename;
                            // Take a snapshot of the file system.
                            System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(startFolder);
                            // This method assumes that the application has discovery permissions
                            // for all folders under the specified path.
                            fileList = dir.GetFiles("*.*", System.IO.SearchOption.AllDirectories);
                            //Create the query
                            IEnumerable<System.IO.FileInfo> fileQuery =
                                from file in fileList
                                where file.Extension == mext
                                orderby file.Name
                                select file;
                            //Execute the query. This might write out a lot of files!
                            foreach (System.IO.FileInfo fi in fileQuery)
                            {
                                AddLeaf(Displaytime);
                                szBmpFile = fi.FullName;
                                LED_AddBmpBlock(nLEDID, szBmpFile, destRect, sourceRect, nEffectType, nSpeed, lTrasnparent);
                            }
                        }
                        #endregion
                        #region==========真正影片===============
                        if (CMMOVIETYPE == "RMOV")
                        {
                            string szPath = rootfolder + mfile + @"\" + mfilename;
                            LED_AddVideoBlock(nLEDID, szPath, destRect);
                        }
                        #endregion
                        break;
                    default:
                        break;
                } 
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.StackTrace, ex.Message);
            }

        }
        public static LEDFontDescriptor GetFontDescription(string cfontid)
        {
            LEDFontDescriptor lpFont = new LEDFontDescriptor();
            try
            {
                //CFSYSTEM	CFOINTID	
                string expression = "CFSYSTEM='000' and CFOINTID='" + cfontid + "'";
                DataRow[] rowsf = LocalData.FONTTABLE.Select(expression);
                DataRow dwFont = null;
                foreach (DataRow dwTemp in rowsf)
                {
                    dwFont = dwTemp;
                }
                if (dwFont == null)
                    return lpFont;
                lpFont.szFontName = dwFont["CFOINTNAME"].ToString().Trim();
                lpFont.lFontSize = System.Convert.ToInt32(dwFont["CFFONTSIZE"].ToString());
                lpFont.lFontColor = System.Convert.ToInt32(dwFont["CFCOLOR"].ToString());
                lpFont.nBold = System.Convert.ToInt32(dwFont["CFFONTBOLD"].ToString());
                lpFont.nUnderline = System.Convert.ToInt32(dwFont["CFFONTUNDERLINE"].ToString());
                lpFont.nStrick = System.Convert.ToInt32(dwFont["CFFONTITALIC"].ToString());
            }
            catch (Exception ex)
            {
            }
            return lpFont;
        }
        public static Int32 LED_EndProgram(int nLEDID, int nProgramID)
        {
            Int32 result = 0;
            try
            {


            }
            catch (Exception ex)
            {
            }
            return result;
        }
        public static void LED_AddTextBlock(int nLEDID, string szText, LEDBlock rect, LEDFontDescriptor lpFont, int nHAlign, int nVAlign, int nEffectType, int nSpeed, Int32 lTrasnparent)
        {
            Int32 result = 0;
            try
            {
                LPRECT rectx = new LPRECT();
                rectx.left = rect.nStartX;
                rectx.top = rect.nStartY;
                rectx.right = rect.nStartX + rect.nWidth;
                rectx.bottom = rect.nStartY + rect.nHeight;
                Int32 method = nEffectType;
                Int32 speed = nSpeed;
                Int32 transparent = lTrasnparent;
                string fontname = lpFont.szFontName;
                Int32 fontsize = lpFont.lFontSize;
                Int32 fontcolor = lpFont.lFontColor;
                AddText(szText, ref rectx, method, nSpeed, transparent, fontname, fontsize, fontcolor);
            }
            catch (Exception ex)
            {
            }
            return;
        }
        public static void LED_AddBmpBlock(int nLEDID, string szBmpFile, LEDBlock destRect, LEDBlock sourceRect, int nEffectType, int nSpeed, Int32 lTrasnparent)
        {
            try
            {
                IntPtr screenDC = GetDC(IntPtr.Zero);
                IntPtr memDC = CreateCompatibleDC(screenDC);
                Bitmap bmp = new Bitmap(szBmpFile);

                IntPtr hBitmap = bmp.GetHbitmap();
                hBitmap = bmp.GetHbitmap();
                SelectObject(memDC, hBitmap); // Select bitmap from compatible bitmap to memDC        
                DeleteObject(hBitmap);
                ReleaseDC(IntPtr.Zero, screenDC);
                ReleaseDC(IntPtr.Zero, memDC);

                SRECT srect = new SRECT();
                Int32 left = destRect.nStartX;
                Int32 top = destRect.nStartY;
                Int32 right = destRect.nStartX + destRect.nWidth;
                Int32 bottom = destRect.nStartY + destRect.nHeight;
                SetRect(ref srect, left, top, right, bottom);
                short w = (short)sourceRect.nWidth;
                short h = (short)sourceRect.nHeight;
                MessageBox.Show(String.Format("left {0:d}:\n right{1}: top {2} degrees (hi)\n bottom{3}: w:{4},h{5}", left, top, right, bottom, w, h));
                AddWindow(memDC, w, h, ref srect, nEffectType, nSpeed, lTrasnparent);
            }
            catch (Exception ex)
            {
            }
            return;
        }
        public static void LED_AddClockBlock(int nLEDID, LEDBlock rect, LEDClockDescriptor lpClock, LEDFontDescriptor lpFont, int nEffectType, int nSpeed, Int32 lTrasnparent)
        {
            Int32 result = 0;

            try
            {
                LPRECT srect = new LPRECT();
                Int32 left = rect.nStartX;
                Int32 top = rect.nStartY;
                Int32 right = rect.nStartX + rect.nWidth;
                Int32 bottom = rect.nStartY + rect.nHeight;
                srect.top = top;
                srect.left = left;
                srect.right = right;
                srect.bottom = bottom;
                AddDateTime(ref srect, lTrasnparent, "楷体", 12, 255, 1);
                MessageBox.Show("AddDateTime");
            }
            catch (Exception ex)
            {
            }
            return;
        }
        public static void LED_AddLine(int nLEDID, int x0, int y0, int x1, int y1, Int32 lColor)
        {
            Int32 result = 0;

            try
            {

            }
            catch (Exception ex)
            {
            }
            return;
        }
        public static void LED_AddAnimationBlock(int nLEDID, string szPath, int nDir, LEDBlock rcRect)
        {
            Int32 result = 0;

            try
            {

            }
            catch (Exception ex)
            {
            }
            return;
        }
        public static void LED_AddVideoBlock(int nLEDID, string szPath, LEDBlock rcRect)
        {
            Int32 result = 0;

            try
            {
                SRECT rect = new SRECT();
                Int32 left = rcRect.nStartX;
                Int32 top = rcRect.nStartY;
                Int32 right = rcRect.nStartX + rcRect.nWidth;
                Int32 bottom = rcRect.nStartY + rcRect.nHeight;
                SetRect(ref rect, left, top, right, bottom);
                string filename = @"D:\ZZMyWorks\SXC\SXCPlatform\SXCPlatform\Exe\LMovie\M00003\WP_20120531.AVI";//szPath;
                Int32 stretch = 0;
                AddMovie(filename, ref rect, stretch);
                IntPtr hdc = GetWindowDC(IntPtr.Zero);
                AddWindow(hdc, 50, 32, ref rect, 1, 1, 1);
                ReleaseDC(hdc, IntPtr.Zero);
            }
            catch (Exception ex)
            {
            }
            return;
        }
        public static int LED_SendToScreen(int nLEDID, int nProgramID)
        {
            int result = 0;

            try
            {
                Int32 dev = 0;
                byte Address = 1;
                DataRow dwLed = (DataRow)LocateLED(nLEDID);
                string Host = dwLed["LMTCPIP4ADDRESS"].ToString();
                ushort port = System.Convert.ToUInt16(dwLed["LMTCPIP4PORT"].ToString());
                LED_SendToScreen(dev, Address, Host, port);  //发送到1组1号显示屏，此处需要根据现场情况修改*/
            }
            catch (Exception ex)
            {
                
                MessageBox.Show(ex.StackTrace, ex.Message);
            }
            return result;
        }
        public static int LED_DeleteProgram(int nLEDID, int nProgramID)
        {
            int result = 0;

            try
            {

            }
            catch (Exception ex)
            {
            }
            return result;
        }
        public static Int32 LED_FastenProgram(int nLEDID)
        {
            Int32 result = 0;

            try
            {

            }
            catch (Exception ex)
            {
            }
            return result;
        }
        [ExportDllAttribute("LED_RunProgram", System.Runtime.InteropServices.CallingConvention.Winapi)]
        public static Int32 LED_RunProgram(int nLEDID, int nProgramID)
        {
            Int32 result = 0;

            try
            {

            }
            catch (Exception ex)
            {
            }
            return result;
        }
        #endregion
        #region========LED Sender.dll==============
        [DllImport("LEDSender.dll", EntryPoint = "LED_Open", CallingConvention = CallingConvention.StdCall)]
        public static extern Int32 LED_Open(ref PDeviceParam param, Int32 Notify, Int32 Window, Int32 Message);

        [DllImport("LEDSender.dll", EntryPoint = "LED_Query", CallingConvention = CallingConvention.StdCall)]
        public static extern void LED_Query(Int32 dev, Byte Address, string Host, UInt16 port);

        [DllImport("LEDSender.dll", EntryPoint = "LED_Close", CallingConvention = CallingConvention.StdCall)]
        public static extern void LED_Close(Int32 dev);

        [DllImport("LEDSender.dll", EntryPoint = "LED_AdjustTime", CallingConvention = CallingConvention.StdCall)]
        public static extern void LED_AdjustTime(Int32 dev, byte Address, string Host, UInt16 port);

        [DllImport("LEDSender.dll", EntryPoint = "LED_SendToScreen", CallingConvention = CallingConvention.StdCall)]
        public static extern void LED_SendToScreen(Int32 dev, byte Address, string Host, UInt16 port);

        [DllImport("LEDSender.dll", EntryPoint = "LED_GetPower", CallingConvention = CallingConvention.StdCall)]
        public static extern void LED_GetPower(Int32 dev, byte Address, string Host, UInt16 port);

        [DllImport("LEDSender.dll", EntryPoint = "LED_SetPower", CallingConvention = CallingConvention.StdCall)]
        public static extern void LED_SetPower(Int32 dev, byte Address, string Host, UInt16 port, int Power);

        [DllImport("LEDSender.dll", EntryPoint = "LED_GetBrightness", CallingConvention = CallingConvention.StdCall)]
        public static extern void LED_GetBrightness(Int32 dev, byte Address, string Host, UInt16 port);

        [DllImport("LEDSender.dll", EntryPoint = "LED_SetBrightness", CallingConvention = CallingConvention.StdCall)]
        public static extern void LED_SetBrightness(Int32 dev, byte Address, string Host, UInt16 port, byte Brightness);

        [DllImport("LEDSender.dll", EntryPoint = "LED_GetNotifyMessage", CallingConvention = CallingConvention.StdCall)]
        public static extern void LED_GetNotifyMessage(PNotifyMessage Notify);

        [DllImport("LEDSender.dll", EntryPoint = "LED_GetOption", CallingConvention = CallingConvention.StdCall)]
        public static extern Int32 LED_GetOption(int Index);

        [DllImport("LEDSender.dll", EntryPoint = "LED_SetOption", CallingConvention = CallingConvention.StdCall)]
        public static extern Int32 LED_SetOption(int Index, Int32 Value);
        /// <summary>
        ///   MakeRoot说明
        ///   Function  MakeRoot(RootType: Integer; ScreenType: Integer): Integer; Stdcall; External LEDSender;
        ///   RootType: 可选ROOT_PLAY或ROOT_DOWNLOAD
        ///    ROOT_PLAY: 表示显示内容仅播放，无需保存，控制卡掉电后不再需要显示
        ///    ROOT_DOWNLOAD: 表示显示内容需要保存在主板中，当控制卡上电后自动播放
        ///    (注意：控制卡只能保存1M的数据)
        ///    ScreenType: 必须和控制卡的设置相同，否则将不会显示
        /// </summary>
        /// <param name="RootType"></param>
        /// <param name="ScreenType"></param>
        /// <returns></returns>
        [DllImport("LEDSender.dll", EntryPoint = "MakeRoot", CallingConvention = CallingConvention.StdCall)]
        public static extern Int32 MakeRoot(Int32 RootType, Int32 ScreenType);

        [DllImport("LEDSender.dll", EntryPoint = "AddLeaf", CallingConvention = CallingConvention.StdCall)]
        public static extern Int32 AddLeaf(Int32 DisplayTime); //DisplayTime单位为毫秒

        [DllImport("LEDSender.dll", EntryPoint = "AddWindow", CallingConvention = CallingConvention.StdCall)]
        public static extern Int32 AddWindow(IntPtr hdc, Int16 width, Int16 height, ref SRECT rect, Int32 method, Int32 speed, Int32 transparent);

        [DllImport("LEDSender.dll", EntryPoint = "AddDateTime", CallingConvention = CallingConvention.StdCall)]
        public static extern Int32 AddDateTime(ref LPRECT rect, Int32 transparent, string fontname, Int32 fontsize, Int32 fontcolor, Int32 format);

        [DllImport("LEDSender.dll", EntryPoint = "AddString", CallingConvention = CallingConvention.StdCall)]
        public static extern Int32 AddString(string str, ref LPRECT rect, Int32 method, Int32 speed, Int32 transparent, Int32 fontset, Int32 fontcolor);

        [DllImport("LEDSender.dll", EntryPoint = "AddText", CallingConvention = CallingConvention.StdCall)]
        public static extern Int32 AddText(string str, ref LPRECT rect, Int32 method, Int32 speed, Int32 transparent, string fontname, Int32 fontsize, Int32 fontcolor);

        [DllImport("LEDSender.dll", EntryPoint = "AddMovie", CallingConvention = CallingConvention.StdCall)]
        public static extern Int32 AddMovie(string filename, ref SRECT rect, Int32 stretch);

        [DllImport("LEDSender.dll", EntryPoint = "AddCountUp", CallingConvention = CallingConvention.StdCall)]
        public static extern Int32 AddCountUp(ref LPRECT rect, Int32 transparent, string fontname, Int32 fontsize, Int32 fontcolor, Int32 format, LPSYSTEMTIME starttime);

        [DllImport("LEDSender.dll", EntryPoint = "AddCountDown", CallingConvention = CallingConvention.StdCall)]
        public static extern Int32 AddCountDown(ref LPRECT rect, Int32 transparent, string fontname, Int32 fontsize, Int32 fontcolor, Int32 format, LPSYSTEMTIME endtime);

        #endregion
        #region===========user32.dll================
        [DllImport("user32.dll")]
        private static extern IntPtr GetDC(IntPtr hwnd);
        [DllImport("user32.dll")]
        private static extern IntPtr ReleaseDC(IntPtr hc, IntPtr hDest);
        [DllImport("user32.dll")]
        private static extern IntPtr GetWindowDC(IntPtr hwnd);
        [DllImport("user32.dll")]
        private static extern IntPtr GetForegroundWindow();
        [DllImport("user32.dll")]
        private static extern int GetWindowRect(IntPtr hwnd, ref Rect lpRect);
        [DllImport("gdi32.dll")]
        private static extern bool BitBlt(IntPtr hdc, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, TernaryRasterOperations dwRop);
        [DllImport("gdi32.dll", ExactSpelling = true, PreserveSig = true, SetLastError = true)]
        static extern IntPtr SelectObject(IntPtr hdc, IntPtr hgdiobj);

        [DllImport("gdi32.dll")]
        private static extern IntPtr CreateCompatibleBitmap(IntPtr hdc, int nWidth, int nHeight);

        [DllImport("gdi32.dll", SetLastError = true)]
        private static extern IntPtr CreateCompatibleDC(IntPtr hdc);

        [DllImport("gdi32.dll")]
        private static extern bool DeleteObject(IntPtr hObject);
        [DllImport("user32.dll", CallingConvention = CallingConvention.StdCall)]
        public static extern Int32 SetRect(ref SRECT rect, Int32 left, Int32 top, Int32 right, Int32 bottom);

        private enum TernaryRasterOperations : uint
        {
            /// <summary>dest = source</summary> 
            SRCCOPY = 0x00CC0020,
            /// <summary>dest = source OR dest</summary> 
            SRCPAINT = 0x00EE0086,
            /// <summary>dest = source AND dest</summary> 
            SRCAND = 0x008800C6,
            /// <summary>dest = source XOR dest</summary> 
            SRCINVERT = 0x00660046,
            /// <summary>dest = source AND (NOT dest)</summary> 
            SRCERASE = 0x00440328,
            /// <summary>dest = (NOT source)</summary> 
            NOTSRCCOPY = 0x00330008,
            /// <summary>dest = (NOT src) AND (NOT dest)</summary> 
            NOTSRCERASE = 0x001100A6,
            /// <summary>dest = (source AND pattern)</summary> 
            MERGECOPY = 0x00C000CA,
            /// <summary>dest = (NOT source) OR dest</summary> 
            MERGEPAINT = 0x00BB0226,
            /// <summary>dest = pattern</summary> 
            PATCOPY = 0x00F00021,
            /// <summary>dest = DPSnoo</summary> 
            PATPAINT = 0x00FB0A09,
            /// <summary>dest = pattern XOR dest</summary> 
            PATINVERT = 0x005A0049,
            /// <summary>dest = (NOT dest)</summary> 
            DSTINVERT = 0x00550009,
            /// <summary>dest = BLACK</summary> 
            BLACKNESS = 0x00000042,
            /// <summary>dest = WHITE</summary> 
            WHITENESS = 0x00FF0062
        }
        #endregion
    }
}
